<?php

namespace app\common\model\banxi\learn;

use app\common\enums\AccountTypeEnum;
use app\common\enums\LatroProficiencyEnum;
use app\common\enums\LearnMeansEnum;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 学练一体
 */
class LearnLatro extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_learn_latro';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'accountTypeCn',
        'proficiencyCn',
        'learnMeansCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'courseId' => 'string',
        'studentId' => 'string',
        'trainerId' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getAccountTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['accountType'] ?: '');
        return (new AccountTypeEnum())->getDescription($value) ?? '';
    }

    public function getProficiencyCnAttr($value, $data)
    {
        $value = $value ?: ($data['proficiency'] ?: '');
        return (new LatroProficiencyEnum())->getDescription($value) ?? '';
    }

    public function getLearnMeansCnAttr($value, $data)
    {
        $value = $value ?: ($data['learnMeans'] ?: '');
        return (new LearnMeansEnum())->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    /**
     * 标记单词熟练度
     */
    public function markProficiency($trainerId, $studentId, $learnMeans, $courseId, $word, $proficiency)
    {
        $accountType = AccountTypeEnum::STUDENT;
        if ($trainerId && $learnMeans == LearnMeansEnum::E2) {
            $accountType = AccountTypeEnum::TRAINER;
        }
        //        var_dump('$trainerId:'.$trainerId);
        //        var_dump('$studentId:'.$studentId);
        //        var_dump('$learnMeans:'.$learnMeans);
        //        var_dump('$courseId:'.$courseId);
        //        var_dump('$word:'.$word);
        //        var_dump('$proficiency:'.$proficiency);
        // 查找是否已存在记录
        $query = $this->where('trainerId', $trainerId)
            ->where('learnMeans', $learnMeans)
            ->where('courseId', $courseId)
            ->where('word', $word);
        if (!$studentId) {
            $query = $query->whereNull('studentId');
        } else {
            $query = $query->where('studentId', $studentId);
        }
        $learnLatro = $query->find();

        if (!$learnLatro) {
            $learnLatro = new self();
            if ($trainerId) $learnLatro->trainerId = $trainerId;
            if ($studentId) $learnLatro->studentId = $studentId;
            $learnLatro->learnMeans = $learnMeans;
            $learnLatro->courseId = $courseId;
            $learnLatro->word = $word;
            $learnLatro->accountType = $accountType;
        }

        $learnLatro->proficiency = $proficiency;
        $learnLatro->save();
    }

    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------


}